package com.allwees.bs.c.module.user.repository;

import com.allwees.bs.c.module.user.entity.StoreCollectionEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface StoreCollectionRepository extends JpaRepository<StoreCollectionEntity, String> {
	@Query(value = "SELECT e FROM StoreCollectionEntity e WHERE userUuid = :userUuid",
			countQuery = "SELECT COUNT(1) FROM StoreCollectionEntity WHERE userUuid = :userUuid")
	Page<StoreCollectionEntity> findCollStore(@Param("userUuid") String userUuid, Pageable pageable);

	@Modifying
	@Query(value = "delete from StoreCollectionEntity where userUuid=:userUuid and storeUuid in :storeUuids")
	void cancelForUser(@Param("userUuid") String userUuid, @Param("storeUuids") List<String> storeUuids);

	@Query(value = "select store_uuid from usr_store_collection where userUuid=:userUuid and storeUuid in :storeUuids", nativeQuery = true)
	List<String> getOnes(@Param("userUuid") String userUuid, @Param("storeUuids") List<String> storeUuids);

	@Query(value = "select count(1) from StoreCollectionEntity e where userUuid=:userUuid and storeUuid = :storeUuid")
	long countByUserAndStoreId(@Param("userUuid") String userUuid, @Param("storeUuid") String storeUuid);

	List<StoreCollectionEntity> findAllByUserUuid(String userUuid);
}
